//
//  DetailViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/22.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "DetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AutomaticImage.h"

@interface DetailViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIImageView *titleImage;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UITextView *msgTextView;
@property (nonatomic,strong)NSMutableArray *needArray;
@property (nonatomic,strong)UIScrollView *scrollView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.leftButton setImage:[UIImage imageNamed:@"个人中心-left@2x"] forState:UIControlStateNormal];
    [self initUi];
}
// 继承父类导航栏封装的点击事件
-(void)action_LeftButtonClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 隐藏下面的分栏标签，进入这个界面的时候隐藏，离开界面的时候显示
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}


-(void)initUi{
    
    if (self.sendArray) {
        if ([self.sendArray[@"imageurls"] count] == 0) {
            self.titleImage.image = [UIImage imageNamed:@"loaded.jpg"];
            [self.scrollView addSubview:self.titleImage];

        }
        else if ([self.sendArray[@"imageurls"] count] == 1 || [self.sendArray[@"imageurls"] count] == 2) {
            self.titleImage.image = [UIImage imageNamed:@"loaded.jpg"];
            
            [self.titleImage sd_setImageWithURL:[NSURL URLWithString:self.sendArray[@"imageurls"][0][@"url"]] placeholderImage:nil];
            
            [self.scrollView addSubview:self.titleImage];
        }
        else{
            
            //遍历数组
            [self.sendArray[@"imageurls"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [self.needArray addObject:obj[@"url"]];
            }];
            
            AutomaticImage *autoimage = [[AutomaticImage alloc]initWithFrame:CGRectMake(0, 0, kwidth, khieght*0.3) dataSource:self.needArray duringTime:2.0];
            
            [self.scrollView addSubview:autoimage];
            
            
        }
        
    self.titleLabel.text = self.sendArray[@"title"];
    self.msgTextView.text = self.sendArray[@"desc"];
    }
    
    [self.scrollView addSubview:self.titleLabel];
    [self.scrollView addSubview:self.msgTextView];
    [self.view addSubview:self.scrollView];

}

#pragma mark -- getter

-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kwidth, khieght-64)];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(kwidth, khieght);
    }
    return _scrollView;
}

-(UIImageView *)titleImage{
    if (_titleImage == nil) {
        _titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(kwidth*0.01, 0, kwidth*0.98, khieght*0.3)];
    }
    return _titleImage;
}

-(UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kwidth*0.15, CGRectGetMaxY(self.titleImage.frame)+khieght*0.02, kwidth*0.7, khieght*0.1)];
        _titleLabel.font = [UIFont systemFontOfSize:19];
        _titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

-(UITextView *)msgTextView{
    if (_msgTextView == nil) {
        _msgTextView = [[UITextView alloc]initWithFrame:CGRectMake(kwidth*0.05, CGRectGetMaxY(self.titleLabel.frame)+khieght*0.03, kwidth*0.9, khieght*0.6)];
        _msgTextView.editable = NO;
        _msgTextView.selectable = NO;
        _msgTextView.font = [UIFont systemFontOfSize:17];
    }
    return _msgTextView;
}

-(NSMutableArray *)needArray{
    if (_needArray == nil) {
        _needArray = [NSMutableArray array];
    }
    return _needArray;
}

@end
